package router

import (
	"apibe/single/src/v1/plugin/tourist"
	"apibe/single/src/v1/router/handler"
	"apibe/single/src/v1/share/middler"
	"fmt"
	"github.com/gin-gonic/gin"
)

// MongoCreateOne 插入一条数据
func MongoCreateOne(c *gin.Context) {
	var one tourist.InsertOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MongoCreateOneHandler(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MongoCreateMany 插入多条数据
func MongoCreateMany(c *gin.Context) {
	var many tourist.InsertMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MongoCreateManyHandler(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MongoQueryOne 查询一条数据
func MongoQueryOne(c *gin.Context) {
	var one tourist.QueryOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MongoQueryOneHandler(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MongoQueryMany 查询多条数据
func MongoQueryMany(c *gin.Context) {
	var many tourist.QueryMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.MSG)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MongoQueryManyHandler(&many)

	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MongoUpdateOne 更新单条数据
func MongoUpdateOne(c *gin.Context) {
	var one tourist.UpdateOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MongoUpdateOneHandler(&one)
	fmt.Println(result)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MongoUpdateMany  更新多条数据
func MongoUpdateMany(c *gin.Context) {
	var many tourist.UpdateMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MongoUpdateManyHandler(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MongoRemoveOne  删除单条数据
func MongoRemoveOne(c *gin.Context) {
	var one tourist.RemoveOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MongoRemoveOneHandler(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MongoRemoveMany  删除多条数据
func MongoRemoveMany(c *gin.Context) {
	var many tourist.RemoveMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MongoRemoveManyHandler(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// RedisSetOne 缓存数据
func RedisSetOne(c *gin.Context) {
	var one tourist.SetOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	err = handler.RedisSetOneHandler(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrRedis)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, "SUCCESS")
}

// RedisSetMany 插入多条缓存(不支持事务)
func RedisSetMany(c *gin.Context) {
	var many tourist.SetMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	errs := handler.RedisSetManyHandler(&many)
	if len(*errs) > 0 {
		c.Set(middler.CODE, middler.ErrRedis)
		c.Set(middler.MSG, errs)
		return
	}
	c.Set(middler.RESULT, "SUCCESS")
}

// RedisGetOne 获取单条缓存
func RedisGetOne(c *gin.Context) {
	var one tourist.GetOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	bytes, err := handler.RedisGetOneHandler(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrRedis)
		c.Set(middler.MSG, err)
		return
	}
	c.Set(middler.RESULT, string(bytes))
}

// RedisGetMany 获取多条缓存
func RedisGetMany(c *gin.Context) {
	var many tourist.GetMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.RedisGetManyHandler(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrRedis)
		c.Set(middler.MSG, err)
		return
	}
	c.Set(middler.RESULT, result)
}

// RedisRemoveOne 删除单条缓存
func RedisRemoveOne(c *gin.Context) {
	var one tourist.DelOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	count, err := handler.RedisDelOneHandler(&one)
	if err != nil {
		c.Set(middler.MSG, err.Error())
	}
	c.Set(middler.RESULT, count)
}

// RedisRemoveMany 删除多条缓存
func RedisRemoveMany(c *gin.Context) {
	var many tourist.DelMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	count, err := handler.RedisDelManyHandler(&many)
	if err != nil {
		c.Set(middler.MSG, err.Error())
	}
	c.Set(middler.RESULT, count)
}

// MysqlSelectOne 查询单条数据
func MysqlSelectOne(c *gin.Context) {
	var one tourist.SelectOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MysqlSelectOne(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMysql)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MysqlSelectMany 查询多条数据
func MysqlSelectMany(c *gin.Context) {
	var many tourist.SelectMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MysqlSelectMany(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMysql)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MysqlExecOne CUD单条SQL执行
func MysqlExecOne(c *gin.Context) {
	var one tourist.ExecOne
	err := c.ShouldBindJSON(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MysqlExecOne(&one)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMysql)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}

// MysqlExecMany CUD多条SQL执行，默认支持事务
func MysqlExecMany(c *gin.Context) {
	var many tourist.ExecMany
	err := c.ShouldBindJSON(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	result, err := handler.MysqlExecMany(&many)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMysql)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, result)
}
